<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.netscenery.yang.model.Deviate"%>
<%@page import="offer.SessionObj"%>
<%@page import="com.netscenery.yang.designer.MapInfo"%>
<%@page import="com.netscenery.yang.designer.FieldMap"%>
<%@page import="com.netscenery.yang.model.BuiltinType"%>
<%@page import="operate.OperateType"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	Object itemNode = request.getAttribute("itemNode");
	Deviate deviate = null;
	if(null != itemNode){
		deviate = (Deviate)itemNode;
	}
	Object opType   = request.getAttribute("opType"); //获取操作对象
	SessionObj sessionObj = (SessionObj)request.getSession().getAttribute("sessionObj");
	String control_disabled = "";
	if(!sessionObj.getModuleStatus().toString().equalsIgnoreCase("EDITING")){
	 	control_disabled ="disabled='disabled'";
	}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>Deviate</title>
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Cache-Control" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/css/items.css" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/css/subItems.css" />
    <script type="text/javascript"  src="<%=basePath%>jstree-v.pre1.0/_lib/jquery.js"></script>
    <script type="text/javascript"  src="<%=basePath%>js/StringUtil.js"></script>
    <script type="text/javascript"  src="<%=basePath%>js/Leaf.js"></script>
  </head>
  
  <body  class="body">
  <script type="text/javascript">
 function bodyLoad(){
 	<% if( (null != itemNode) 
        &&  (!opType.toString().equalsIgnoreCase(OperateType.LOAD))){ %>
    <%   		 if(opType.toString().equalsIgnoreCase(OperateType.ADD)
	           || opType.toString().toString().equalsIgnoreCase(OperateType.MODIFY_INCLUDE_NAME)){ %>
	   		 	 //新增节点时，重新加载数
	   		 	 parent.reloadTreeWithNode(document.getElementById("nodeId").value);
    <%   		 } %>
			 $("#opNoticeSpan").html("操作成功！");
   		 	 setTimeout(function(){$("#opNoticeSpan").html("")},3000);
   <% }%>
   		if($("#_derivedType").html().trim().length != 0){
	 		$("#DerivedType_DIV").css("display", "block");
			$("#DerivedType_DIV").addClass("itemDiv");
			$("#FrashDerivedButton").css("display", "");
 		} 
 }
 
 $(document).ready(function(){
     $("#submitButton").click(function(){
	     $(":input").css("border-color","");
     	 if($("#_name").val().trim().length == 0){
         $("#_name").css("border-color","red");
         alert("name 不能为空！");
         return;
     }
     
     if($("#_baseType").val().trim().length == 0){
        $("#_baseType").css("border-color","red");
        alert("Type 不能为空！");
        return;
     }
    document.getElementById("deviateForm").submit(); 
	});
	bodyLoad();
 });
  function Show_TypeEdit(){
 	var baseType    = document.getElementById("_baseType").value;
 	var derivedType = document.getElementById("_derivedType").value;
 	if("" == baseType){
 	    alert("Type:不能为空！");
 	    return false;
 	}
 	var url = "<%=basePath%>GoToFindDeviateTypeServlet?parent_nodeId="+document.getElementById("nodeId").value+"&baseType="+baseType+"&derivedType="+derivedType;
 	parent.CreateType(url);
 } 	
</script>
    <form id="deviateForm" method="post" action="<%=basePath %>DeviateOperateServlet" class="IndexFormStyle" >
   		<input type="hidden"  id="nodeId" name="nodeId" value="<%=request.getAttribute("nodeId") %>"  />
   		<div id="TableContent" class="typeSubContent" >
	      <div class="itemTitle">
	          The [Deviate] information 
	      </div>
	      <% MapInfo mapInfo = null;
	       try{
	       		mapInfo = deviate.getMapInfo();
	       }catch(Exception e){
				System.out.print(e.getMessage()+":"+e.toString());
	       }
	       FieldMap fieldMap = null;
	       boolean hasConfig       = false;
	       boolean hasDefaultValue = false;
	       boolean hasMandatory    = false;
	       boolean hasMaxElements  = false;
	       boolean hasMinElements  = false;
	       boolean hasType         = false;
	       boolean hasUnique       = false;
	       boolean hasUnits        = false;
	       if(null != mapInfo){
	       		for(int i = 0 ; i < mapInfo.getBasicFieldsNumber() ; i++ ){
	       			fieldMap = mapInfo.getBasicField(i);
	       			if(fieldMap.getFieldName().equalsIgnoreCase("config")){
         				hasConfig = true;
         			}
         			if(fieldMap.getFieldName().equalsIgnoreCase("mandatory")){
         				hasMandatory = true;
         			}
         			if(fieldMap.getFieldName().equalsIgnoreCase("defaultValue")){
         				hasDefaultValue = true;
         			}
         			if(fieldMap.getFieldName().equalsIgnoreCase("maxElements")){
         				hasMaxElements = true;
         			}
         			if(fieldMap.getFieldName().equalsIgnoreCase("minElements")){
         				hasMinElements = true;
         			}
         			if(fieldMap.getFieldName().equalsIgnoreCase("unique")){
         				hasUnique = true;
         			}
         			if(fieldMap.getFieldName().equalsIgnoreCase("type")){
         				hasType = true;
         			}
         			if(fieldMap.getFieldName().equalsIgnoreCase("units")){
         				hasUnits = true;
         			}
	       		}
	       }
	       %>
	       <div class="itemDiv">
		     <span class="textSpan">&nbsp;Deviate name:</span>
		     <input class="inputText" id="_name" name="_name" value="<%=deviate.getName()==null?"":deviate.getName()%>" readonly="readonly"  />
		   </div>
		   <%if(hasConfig){ %>
		   <div class="itemDiv">
		      <% if(!deviate.isConfig()){ %>
		          &nbsp;<input type="checkbox" id="isConfig" name = "isConfig"  value="true" />Config
		         <%} else{%> 
		          &nbsp;<input type="checkbox" id="isConfig" name = "isConfig" checked="checked" value="true" />Config
		         <%}%> 
		   </div>
		   <%} 
		     if(hasDefaultValue){
		   %>
		   <div class="itemDiv">
		     <span class="textSpan">&nbsp;Default:</span>
		     <input class="inputText" id="_default" name="_default" value="<%=deviate.getDefaultValue()==null?"":deviate.getDefaultValue()%>"  />
		   </div>
		   <%} 
		     if(hasMandatory){
		   %>
		   <div class="itemDiv">
		     <% if(!deviate.isMandatory()){ %>
		          &nbsp;<input type="checkbox" id="isMandatory" name = "isMandatory"  value="true" />Mandatory
		         <%} else{%> 
		          &nbsp;<input type="checkbox" id="isMandatory" name = "isMandatory" checked="checked" value="true" />Mandatory
		         <%}%> 
		   </div>
		   <%}
		     if(hasType){
		    %>
		   <div class="itemDiv">
		     <span class="textSpan">&nbsp;Type:</span>
		     <select style="width:305px;   height:23px;" onchange="getDerivedType(this.value)"   class="inputSelect" id="_baseType" name="_baseType">
		          <% ArrayList<String> baseTypeList = BuiltinType.getBuiltinTypes(); %>
		          <% if(null ==deviate.getType() || null ==deviate.getType().getBase()){ %>
		          	   <option  value="" ></option>  
		          <% } %>
		          <% for(int i = 0 ; i < baseTypeList.size(); i++ ){ %>
		          <%     if(null !=deviate.getType()&& null !=deviate.getType().getBase()&& baseTypeList.get(i).equalsIgnoreCase(deviate.getType().getBase().getFieldName())){ %>  
		                    <option  value="<%= baseTypeList.get(i)%>" selected="selected"><%=baseTypeList.get(i) %></option>
		          <%     }else{ %> 
		                    <option  value="<%= baseTypeList.get(i)%>" ><%=baseTypeList.get(i) %></option>   
		          <%     }
		             } %>
		     </select>
		     <input type="button" id="_typeEdit" value="Edit Type" onclick="Show_TypeEdit()" style="display:" />
		     <span class="notNullSpan">*</span>
		     <input type="hidden" id="_typehideValue" value="" />
		   </div>
		   <div id="DerivedType_DIV" style="display:none;" >
		     <span class="textSpan">&nbsp;DerivedType:</span>
		     <select style="width:305px;   height:23px;"   class="inputSelect" id="_derivedType" name="_derivedType">
		      <% if(null !=deviate.getType()&& deviate.getType().isDerivedType()){ %>
	             <option  value="<%= deviate.getType().getDerived().getName() %>" selected="selected"><%=deviate.getType().getName() %></option>
		     <% } %>
		     </select>&nbsp;<input type="button" id="FrashDerivedButton" style="display:none;" value = " Modify " onclick ="FrashDerivedType()" />
		    
		   </div>
		   <%} 
		     if(hasUnits){
		   %>
		    <div class="itemDiv">
		     <span class="textSpan">&nbsp;Units:</span>
		     <input class="inputText" id="_units" name="_units" value="<%=deviate.getUnits()==null?"":deviate.getUnits()%>"  />
		   </div>
	    </div>
	    <%} %>
	    <div id="submitContent" class="submitContent">
	     <span id="opNoticeSpan" style="color:red;" ></span>
	     <span class="textSpan">&nbsp;</span>
	     <input type="button" id="submitButton" <%=control_disabled %> value="Apply"  />
	   </div>
   	</form>
  </body>
</html>
